package com.atguigu.eduservice.controller.admin;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.vo.TeacherQuery;
import com.atguigu.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-02-24
 */
@Api(description = "讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
@CrossOrigin
public class EduTeacherController {

    @Autowired
    private EduTeacherService teacherService;

    @PostMapping("getNextSort")
    public R getNextSort() {
        Long sort = teacherService.getNextSort();
        return R.ok().data("sort", sort);
    }

    @ApiOperation(value = "所有讲师列表")
    @PostMapping("findAll")
    public R findAllTeacher() {
        //调用service的方法实现查询所有的操作
        List<EduTeacher> list =
                teacherService.list(Wrappers.<EduTeacher>lambdaQuery().orderByAsc(EduTeacher::getSort)
                        .select(EduTeacher::getName, EduTeacher::getCareer, EduTeacher::getId));
        return R.ok().data("items", list);
    }

    @ApiOperation(value = "逻辑删除讲师")
    @PostMapping("{id}")
    public R removeTeacher(@ApiParam(name = "id", value = "讲师ID", required = true)
                           @PathVariable String id) {
        boolean flag = teacherService.removeById(id);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @PostMapping("pageTeacher/{current}/{limit}")
    public R pageListTeacher(@PathVariable long current,
                             @PathVariable long limit,
                             @RequestBody(required = false) TeacherQuery teacherQuery) {
        //创建page对象
        Page<EduTeacher> pageTeacher = new Page<>(current, limit);
        LambdaQueryWrapper<EduTeacher> query =
                Wrappers.<EduTeacher>lambdaQuery()
                        .eq(ObjectUtils.isNotNull(teacherQuery.getLevel()), EduTeacher::getLevel, teacherQuery.getLevel())
                        .like(ObjectUtils.isNotNull(teacherQuery.getName()), EduTeacher::getName, teacherQuery.getName())
                        .between(ObjectUtils.isNotNull(teacherQuery.getStartTime(), teacherQuery.getEndTime()),
                                EduTeacher::getGmtCreate, teacherQuery.getStartTime(), teacherQuery.getEndTime())
                        .orderByAsc(EduTeacher::getSort);

        teacherService.page(pageTeacher, query);
        long total = pageTeacher.getTotal();
        List<EduTeacher> records = pageTeacher.getRecords();
        return R.ok().data("total", total).data("rows", records);
    }


    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher) {
        boolean save = teacherService.save(eduTeacher);
        if (save) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @PostMapping("getTeacher/{id}")
    public R getTeacher(@PathVariable String id) {
        EduTeacher eduTeacher = teacherService.getById(id);
        return R.ok().data("teacher", eduTeacher);
    }

    @PostMapping("updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher) {
        boolean flag = teacherService.updateById(eduTeacher);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @PostMapping("addTeacher/batch")
    public R batch(@RequestBody List<EduTeacher> eduTeachers) {
        boolean save = teacherService.saveBatch(eduTeachers);
        if (save) {
            return R.ok();
        } else {
            return R.error();
        }
    }
}

